package chkui.springcore.example.hybrid.beanmanipulation.bean;

import java.beans.PropertyEditorSupport;

public class TyreEditor extends PropertyEditorSupport {
	private String FLAG_1 = "/";
	private String FLAG_2 = "R";
	private String FLAG_3 = " ";

	public void setAsText(String text) {
		Tyre tyre = new Tyre();
		Result result = getParam(text, FLAG_1);
		tyre.setWidth(result.dim);
		result = getParam(result.substr, FLAG_2);
		tyre.setFlatRatio(result.dim);
		tyre.setRim(Integer.parseInt(result.substr.replace(FLAG_3, "")));
		setValue(tyre);
	}
	
	private Result getParam(String text, String flag) throws IllegalArgumentException {
		int pos = text.indexOf(flag);
		if (-1 == pos) {
			throw new IllegalArgumentException("参数格式错误");
		}
		String dim = text.substring(0, pos);
		Result r = new Result();
		r.dim = Integer.parseInt(dim);
		r.substr = text.replace(dim + flag, "");
		return r;
	}
}

class Result {
	int dim;
	String substr;
}
